  /**
   * 
   * @module description:后端从数据库根据不同信息获取地块不同信息进行展示
   * @author 吴迪嘉
   */
const theDatabase = require('../../config/db.js').theDb; //引入数据库
const sample_detector_ground_info_Schema = theDatabase.import('../../schema/sample_lab_ground_info.js'); 
const common_model = require('../../models/common/Map.js')
var reference, element_Map

common_model.then(data => {
  reference = data.reference_17_ground_Map
  element_Map = data.element_Map
})

class SampleModel {
    static async getDataByPointnum(point_num) {


    const AllProjectData = await sample_detector_ground_info_Schema.findAll({
        where:{
            point_num: point_num
        }
    })

    return AllProjectData
}
static async getAttentionByPointnum(point_num) {


    // const list=new Set()
    var list=[]
    var returnlist=[]
    const AllProjectData = await sample_detector_ground_info_Schema.findAll({
        where:{
            point_num: point_num
        }
    })
    let res2 = AllProjectData.map(item=>{
        return item = item.dataValues
      })
    for(let item of res2){
        
        
        list.push(item.attention)
    }
    for(let i of list){
        var temp=i.split(',')
        for(let y of temp){
            returnlist.push(y)
        }
    }
    // console.log(returnlist)
    var x = new Set(returnlist);
var xlist=Array.from(x)

    return xlist

}
static async getAttentionByAssessTypePointnum(assess_type,point_num) {


    // const list=new Set()
    var list=[]
    var returnlist=[]
    const AllProjectData = await sample_detector_ground_info_Schema.findAll({
        where:{
            assess_type:assess_type,
            point_num: point_num
        }
    })
    if(AllProjectData==""){
        return -1;
    }
    let res2 = AllProjectData.map(item=>{
        return item = item.dataValues
      })
    for(let item of res2){
        
        
        list.push(item.attention)
    }
    // console.log(1122,list)
    for(let i of list){
        var temp=i.split(',')
        for(let y of temp){
            returnlist.push(y)
        }
    }

    var x = new Set(returnlist);

var xlist=Array.from(x)

    return xlist

}
static async getAlldata() {


    const AllProjectData = await sample_detector_ground_info_Schema.findAll({
       
    })

    return AllProjectData
}

static async UpdateCount(id,sample_num,assess_type,count) {

    await sample_detector_ground_info_Schema.update({
        count:count,      
    },{
        where:{
            point_num:id,
            sample_num:sample_num,
            assess_type:assess_type,
        }
    })
    return true
}
static async UpdateAttention(point_num,sample_num,attention) {



    await sample_detector_ground_info_Schema.update({
        attention:attention,      
    },{
        where:{
            point_num:point_num,
            sample_num:sample_num,
        }
    })
    return true
}
static async AddNewData(data) {

    const result = await sample_detector_ground_info_Schema.create({
        sample_num: data.sample_num,
        assess_type: data.assess_type,
        point_num: data.point_num,
        project_num: data.project_num,
        date: data.date,
        sample_depth: data.sample_depth,
        PH: data.PH,
        arsenic: data.arsenic,
        cadmium: data.cadmium,
        chromium: data.chromium,
        copper: data.copper,
        lead: data.lead,
        mercury:data.mercury,
        nickel:data.nickel,
        antimony:data.antimony,
        beryllium:data.beryllium,
        cobalt:data.cobalt,
        zinc:data.zinc,
        silver:data.silver,
        thallium:data.thallium,
        tin:data.tin,
        selenium:data.selenium,
        molybdenum:data.molybdenum,
        Alum:data.Alum,
        remarks:data.remarks,
        lng:data.lng,
        log:data.log,
        count:data.count,
        attention:data.attention,
      })
      return result
      
}


static async GetMaxAttentionAndCountByPointnumAssessType(point_num,assess_type) {


    const result =  await sample_detector_ground_info_Schema.findAll({
        
        where:{
            point_num:point_num,
            assess_type:assess_type,
        }
    })
    let res2 = result.map(item=>{
        return item = item.dataValues
      })
      var list={}

      for(let i of res2){
        // console.log(i.attention)
        var s=i.attention.split(",")
        for(let j of s){
            if(!list[j]){
                list[j]=1
            }
            else{
                list[j]=list[j]+1
            }


        }
    }
    // console.log(list)
    var max=0
    var Attention=0
    var AttentionAndCount=[]
    for(var j in list){
        if(list[j]>max){
              max=list[j]
              Attention=j
        }

    }
    AttentionAndCount.push({attention:Attention,Count:list[Attention]})
    // console.log(AttentionAndCount)
    return AttentionAndCount
}


static async getSampleNumByProjectnum(project_num) {


    const AllProjectData = await sample_detector_ground_info_Schema.findAll({
        where:{
            project_num: project_num
        }
    })
    
    let ProjectData = AllProjectData.map(item=>{
        return item = item.dataValues
      })
    //   console.log("这是AllProjectData",ProjectData)
    return ProjectData.length
    
    }


//2.0 改为超标倍数
    static async GetMaxByPointnumAssessType(point_num,assess_type) {
        const result =  await sample_detector_ground_info_Schema.findAll({
            where:{
                point_num:point_num,
                assess_type:assess_type,
            }
        })
        //得到该监测点下该监测类型的数据
        let res2 = result.map(item=>{
            return item = item.dataValues
          })
          var list={}
    
          for(let i of res2){
            // console.log(i.attention)
            var s=i.attention.split(",")
            for(let j of s){
                if(!list[j]){
                    list[j]=i[j]/reference.get(j);
                }
                else{
                    list[j]=Math.max(list[j],i[j]/reference.get(j))
                }
    
    
            }
        }
        // console.log(list)
        var max=0
        var Attention=0
        var AttentionAndCount=[]
        for(var j in list){
            if(list[j]>max){
                  max=list[j]
                  Attention=j
            }
    
        }
        AttentionAndCount.push({attention:Attention,Count:list[Attention]})
        // console.log(AttentionAndCount)
        return AttentionAndCount
    }
    

}


module.exports = SampleModel